package view
{
	import com.bit101.components.HBox;
	
	import component.Image;
	import component.LContainer;
	
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import utils.DrawUtils;
	
	public class CanvasFrame extends BaseFrame
	{
		
		private static var instance:CanvasFrame; 
		
		private var content:HBox;
		private var imgContainer:LContainer;
		private var maxW:int = 710;
		private var easing:Number=0.2; 
		private var tx:Number;
		private var tip:CanvasTip;
		public var showDetail:ShowDetail;
		public function CanvasFrame()
		{
			if(instance!=null) throw new Error("Error: Singletons can only be instantiated via getInstance() method!");  
			CanvasFrame.instance = this;  
			init();
		}
		public static function getInstance():CanvasFrame  
		{  
			if(instance==null)  instance = new CanvasFrame();  
			return instance; 
		}
		private function init():void{
			setInfo("<font color='#6d5939' size='16' face='黑体' ><b>画框说明</b></font>");
			imgContainer = new LContainer(this,30,92);
			imgContainer.width = maxW;
			imgContainer.height = 200;
			content = new HBox(imgContainer);
			imgContainer.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			tip = new CanvasTip;
			this.addChild(tip);
			tip.x = maxW * 0.5 + 30;
			tip.y = 60;
			tip.alpha = 0;
			tip.mouseEnabled = false;
			tip.mouseChildren = false;
			showDetail = new ShowDetail();
			
		}
		public function setData(ary:Array):void{
			if(ary == null){
				CanvasEdit(this.parent).container.onClick();
				return;
			}
			while(content.numChildren){
				content.removeChildAt(0);
			}
			content.x = 0;
			for(var i:int = 0;i<ary.length;i++){
				var img:Image = new Image();
				img.setSource(ary[i].pic);
				img.data = ary[i];
				img.addTxt(ary[i].name);
				content.addChild(img);
			}
		}
		public function clear():void{
			while(content.numChildren){
				content.removeChildAt(0);
			}
			tip.alpha = 0;
		}
		private function onMouseMove(event:MouseEvent):void{
			if(content.width < maxW){
				return;
			}
			tx = (imgContainer.mouseX/imgContainer.width)*(-content.width + imgContainer.width);
			if(imgContainer.mouseX < 5){
				tx += 5
			}else if(imgContainer.mouseX > (maxW-5)){
				tx -= 5
			}
			this.addEventListener(Event.ENTER_FRAME,en);
			/*if(imgContainer.mouseY < 0 ||
				imgContainer.mouseY > imgContainer.height ||
				imgContainer.mouseX < 0 ||
				imgContainer.mouseX > imgContainer.width ){
				tip.visible = false;
			}*/
		}
		public function runClick(xx:Number,data:Object,bitmap:Bitmap):void{
			imgContainer.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			moveTo(maxW*0.5 -xx);
			tip.show(data,bitmap);
		}
		public function runOver():void{
			imgContainer.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			if(content.width < maxW){
				moveTo(0);
			}
		}
		public function moveTo(xx:Number):void{
			tx = xx;
			this.addEventListener(Event.ENTER_FRAME,en);
		}
		private function en(event:Event):void{
			var vx:Number=(tx - content.x) * easing; 
			content.x+= vx; 
			//trace(Math.abs(content.x - tx))
			if(Math.abs(content.x - tx) < 2.5){
				content.x = tx;
				this.removeEventListener(Event.ENTER_FRAME,en);
			}
		}
		private function onMouseOut():void{
			tip.visible = false;
		}
		/*public function showTip():void{
			
		}*/
		public function hideTip():void{
			tip.visible = false;
		}
		public function showDetailImg():void{
			this.addChild(showDetail);
			showDetail.x = 30;
			showDetail.y = 20;
		}
	}
}